package com.my.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.my.dao.BaseMapper;
import com.my.enums.CommonOperatorEnum;
import com.my.exception.ServiceException;
import com.my.utils.SimplePage;

public abstract class BaseServiceImpl implements BaseService {

	private BaseMapper mapper;

	@PostConstruct
	private void initConfig() {
		this.mapper = init();
	}

	public abstract BaseMapper init();

	public <ModelType> int deleteById(ModelType modelType) throws ServiceException {
		try {
			return this.mapper.deleteByPrimarayKeyForModel(modelType);
		} catch (Exception e) {
			throw new ServiceException(new StringBuilder().append("Problem invoking method, Cause:")
					.append(e.getMessage()).toString(), e);
		}
	}

	public <ModelType> int add(ModelType modelType) throws ServiceException {
		try {
			return this.mapper.insertSelective(modelType);
		} catch (Exception e) {
			throw new ServiceException(new StringBuilder().append("Problem invoking method, Cause:")
					.append(e.getMessage()).toString(), e);
		}
	}

	public <ModelType> ModelType findById(ModelType modelType) throws ServiceException {
		try {
			return this.mapper.selectByPrimaryKey(modelType);
		} catch (Exception e) {
			throw new ServiceException(new StringBuilder().append("Problem invoking method, Cause:")
					.append(e.getMessage()).toString(), e);
		}
	}

	public <ModelType> List<ModelType> findByBiz(ModelType modelType, Map<String, Object> params)
			throws ServiceException {
		try {
			return this.mapper.selectByParams(modelType, params);
		} catch (Exception e) {
			throw new ServiceException(new StringBuilder().append("Problem invoking method, Cause:")
					.append(e.getMessage()).toString(), e);
		}
	}

	public <ModelType> int modifyById(ModelType modelType) throws ServiceException {
		try {
			return this.mapper.updateByPrimaryKeySelective(modelType);
		} catch (Exception e) {
			throw new ServiceException(new StringBuilder().append("Problem invoking method, Cause:")
					.append(e.getMessage()).toString(), e);
		}
	}

	public int findCount(Map<String, Object> params) throws ServiceException {
		try {
			return this.mapper.selectCount(params);
		} catch (Exception e) {
			throw new ServiceException("", e);
		}
	}

	public <ModelType> List<ModelType> findByPage(SimplePage page, String orderByField, String orderBy,
			Map<String, Object> params) throws ServiceException {
		try {
			return findByPage(page, orderByField, orderBy, params);
		} catch (ServiceException e) {
			throw e;
		}
	}

	@SuppressWarnings("rawtypes")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { ServiceException.class })
	public <ModelType> int save(Map<CommonOperatorEnum, List<ModelType>> params) throws ServiceException {
		try {
			int count = 0;
			for (Map.Entry param : params.entrySet()) {
				Iterator localIterator2;
				if (((CommonOperatorEnum) param.getKey()).equals(CommonOperatorEnum.DELETED)) {
					List list = (List) params.get(CommonOperatorEnum.DELETED);
					if ((null != list) && (list.size() > 0)) {
						for (localIterator2 = list.iterator(); localIterator2.hasNext();) {
							Object modelType = localIterator2.next();
							count += this.mapper.deleteByPrimarayKeyForModel(modelType);
						}
					}
				}
				if (((CommonOperatorEnum) param.getKey()).equals(CommonOperatorEnum.UPDATED)) {
					List list = (List) params.get(CommonOperatorEnum.UPDATED);
					if ((null != list) && (list.size() > 0)) {
						for (localIterator2 = list.iterator(); localIterator2.hasNext();) {
							Object modelType = localIterator2.next();
							count += this.mapper.updateByPrimaryKeySelective(modelType);
						}
					}
				}
				if (((CommonOperatorEnum) param.getKey()).equals(CommonOperatorEnum.INSERTED)) {
					List list = (List) params.get(CommonOperatorEnum.INSERTED);
					if ((null != list) && (list.size() > 0)) {
						for (localIterator2 = list.iterator(); localIterator2.hasNext();) {
							Object modelType = localIterator2.next();
							this.mapper.insertSelective(modelType);
						}
						count += list.size();
					}
				}
			}
			return count;
		} catch (Exception e) {
			throw new ServiceException("", e);
		}
	}
}